<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIHeadStart Array Helpers
 *
 * @author Casey McLaughlin
 * @package CIHeadStart
 * @link http://code.google.com/p/ciheadstart
 */

// --------------------------------------------------------------------

/**
 * Removes the empty elements from either side of an array
 *
 * @param array $array
 * @return array
 */
function array_trim($array)
{
	foreach($array as &$item)
		$item = trim($item);

	$first_value = array_shift($array);
	if ($first_value != '' && $first_value != NULL)
		array_unshift($array, $first_value);

	$last_value = array_pop($array);
	if ($last_value != '' && $last_value != NULL)
		array_push($array, $last_value);

	return $array;
}

// --------------------------------------------------------------------

/**
 * Returns the key for the first value in the array matching
 * the parameter $value
 *
 * If there are multiple items in the array, this function will return
 * only the first found.  Use $offset to start searching the array only after
 * a certain value.
 *
 * @param array $array
 * @param mixed $value
 * @param int|null $offset
 * @return string
 */
function key_for_value($array, $value, $offset = NULL)
{
	if (is_int($offset))
		$array = array_slice($array, $offset);

	foreach($array as $key => $item)
	{
		if ($item == $value)
			return $key;
	}

	//If made it here
	return FALSE;
}

// --------------------------------------------------------------------

/**
 * Sets all of the keys in the array equal to their corresponding values.
 *
 * @param array $array
 * @return array
 */
function set_keys_equal_to_values($array)
{
	$out_array = array();

	foreach($array as $item)
		$out_array[$item] = $item;

	return $out_array;
}

// --------------------------------------------------------------------

/**
 * Checks to see if an array is associative
 *
 * This function will return TRUE if the array contains keys that are
 * not incrementing integers
 *
 * @param array $array
 * @return boolean
 */
function is_assoc($array) 
{
    return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
}

// --------------------------------------------------------------------

function array_remove($array, $value, $preserve_keys = FALSE)
{
	$out_array = array();
	foreach($array as $k => $v)
	{
		if ($value !== $v)
		{
			if ($preserve_keys)
				$out_array[$k] = $v;
			else
				$out_array[] = $v;
		}
	}

	return $out_array;
}

// --------------------------------------------------------------------

/**
 * Computes the difference between two arrays (associatve or iterative)
 *
 * This function will return TRUE if the arrays match, or FALSE if they
 * do not.
 *
 * Optionally, this function will return the number of differences in the two
 * arrays if $return_diff_int is set to TRUE.
 *
 * @param array|object $array_a
 * @param array|object $array_b
 * @param boolean $return_diff_int
 * @return boolean|int
 */
function array_compare($array_a, $array_b, $return_diff_int = FALSE)
{
	foreach($array_a as &$i)
	{
		if (is_object($i))
			$i = serialize($i);
	}
	foreach ($array_b as &$i)
	{
		if (is_object($i))
			$i = serialize($i);
	}
	
	
	if (is_assoc($array_a) OR is_assoc($array_b))
		$diff_int = count(array_diff_assoc($array_a, $array_b));
	else
		$diff_int = count(array_diff($array_a, $array_b));
	
	if ($return_diff_int)
		return $diff_int;
	elseif ($diff_int == 0)
		return TRUE;
	else
		return FALSE;
}

/* End of file MY_array_helper.php */
/* Location: ./app/helpers/MY_array_helper.php */